package com.bit.mybatis.demo.mapper;

import com.bit.mybatis.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper2 {

    @Select("SELECT * FROM `user_info` where id = #{id}")
    UserInfo selectById(Integer id);

    @Select("SELECT * FROM `user_info` where id = ${id}")
    UserInfo selectById2(Integer id);


    @Select("SELECT * FROM `user_info` where username = #{name}")
    UserInfo selectByName(String name);

    @Select("SELECT * FROM `user_info` where username = '${name}'")
    UserInfo selectByName2(String name);


    @Select("SELECT * FROM `user_info` where age = #{age} and gender = #{gender}")
    List<UserInfo> selectByAgeAndGender(Integer age, Integer gender);


    @Select("SELECT * FROM `user_info` where age = #{age} and gender = #{gender}")
    List<UserInfo> selectByAgeAndGender2(Integer age, @Param("gender") Integer gen);


    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age) VALUE(#{username}, #{password}, #{age} )")
    Integer insertUser(UserInfo userInfo);

    @Insert("insert into user_info (username, `password`, age) VALUE(#{userInfo.username}, #{userInfo.password}, #{userInfo.age} )")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);


    @Delete("delete from user_info where id = #{id}")
    Integer deleteUser(Integer id);

    @Update("update user_info set gender = #{gender}, delete_flag = #{deleteFlag}  where id = #{id}")
    Integer updateUser(UserInfo userInfo);


    @Select("select * from user_info ORDER BY id ${order}")
    List<UserInfo> selectUserByOrder(String order);


    @Select("select * from user_info where username like concat('%', #{name}, '%')")
    List<UserInfo> selectUserByLike(String name);

    @Update("<script> update user_info" +
            "<set>" +
            "<if test='gender!=null'>  gender = #{gender}, </if>" +
            "<if test='deleteFlag!=null'> delete_flag = #{deleteFlag} </if>" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    Integer updateUserByCondition(UserInfo userInfo);


}
